/*
 * Separator.java  0.4.0 / Oct 26, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.swing;

import com.cute4j.swing.properties.SeparatorProperties;

/**
 * Separator Component.
 *
 * @cute4j.native
 *    generate="header"
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public class Separator extends Component
                         implements SwingConstants, SeparatorProperties {

// Static Initialization. ------------------------------------------------------

  static {
    _initialize();
  }

// Package-Private Static Fields. ----------------------------------------------

  static final int DEFAULT_ORIENTATION = HORIZONTAL;

// Private Fields. -------------------------------------------------------------

  private int orientation;
  private String text;

// Public Constructors. --------------------------------------------------------

  /**
   * Constructs new Separator object.
   */
  public Separator() {
    this(null, DEFAULT_ORIENTATION);
  }

  /**
   * Constructs new Separator object.
   *
   * @param text the text
   */
  public Separator(String text) {
    this(text, DEFAULT_ORIENTATION);
  }

  /**
   * Constructs new Separator object.
   *
   * @param orientation the orientation of the separator
   */
  public Separator(int orientation) {
    this(null, orientation);
  }

  /**
   * Constructs new Separator object.
   *
   * @param text the text
   * @param orientation the orientation of the separator
   */
  public Separator(String text, int orientation) {
    if ((orientation < HORIZONTAL) || (orientation > VERTICAL)) {
      throw new IllegalArgumentException("Invalid orientation");
    }
    if (orientation != DEFAULT_ORIENTATION) {
      _setOrientation(_delegate, VERTICAL); // NOPMD
    }
    this.orientation = orientation;
    if (text != null) {
      this.text = text;
      _setText(_delegate, text); // NOPMD
    }
  }

// Public Methods. -------------------------------------------------------------

  /**
   * Returns orientation of the separator.
   *
   * @return orientation of the separator
   */
  public int getOrientation() {
    return orientation;
  }

  /**
   * Sets orientation.
   *
   * @param orientation the orientation
   */
  public void setOrientation(int orientation) {
    if ((orientation < HORIZONTAL) || (orientation > VERTICAL)) {
      throw new IllegalArgumentException("Invalid orientation");
    }
    final int oldOrientation = this.orientation;
    if (oldOrientation != orientation) {
      disposedCheck();
      _setOrientation(_delegate, orientation);
    }
  }

  /**
   * Returns text.
   *
   * @return text
   */
  public String getText() {
    return text;
  }

  /**
   * Sets text.
   *
   * @param text the text
   */
  public void setText(String text) {
    final String oldText = this.text;
    if (oldText == null) {
      if (text == null) {
        return;
      }
    } else {
      if (oldText.equals(text)) {
        return;
      }
    }
    disposedCheck();
    _setText(_delegate, text);
    this.text = text;
    firePropertyChange(PROPERTY_TEXT, oldText, text);
  }

// Native Callbacks. -----------------------------------------------------------

  void _orientationChanged(int orientation) {
    final int oldOrientation = this.orientation;
    if (oldOrientation != orientation) {
      this.orientation = orientation;
      firePropertyChange(PROPERTY_ORIENTATION, oldOrientation, orientation);
    }
  }

// Package-Private Native Methods. ---------------------------------------------

  native long _create();

  native void _setOrientation(long delegate, int orientation);

  native void _setText(long delegate, String text);

// Private Static Native Methods. ----------------------------------------------

  private static native void _initialize();

}
